﻿using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base;
using DevExpress.Xpo;
using DevExpress.Persistent.Validation;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.ConditionalAppearance;

namespace DomainComponents.XCRM {
    public enum InvoiceStatus {
        None = 0,
        Canceled = 1,
        Completed = 2,
        Partial = 3
    }

    [DomainComponent]
    [ImageName("BO_Sale")]
    [VisibleInReports]
    [Appearance("Disable Paid Invoice", TargetItems = "*", Criteria = "[Status] = 'Completed'", Enabled = false)]
    [ListViewAutoFilterRowAttribute(true)]
    [XafDisplayName("Invoice")]
    public interface IInvoice : ISaleBase {
        InvoiceStatus Status { get; set; }

        [VisibleInListView(false)]
        DateTime DueDate { get; set; }
        
        [Action(PredefinedCategory.View, Caption = "Close Invoice...", AutoCommit = true,
            TargetObjectsCriteria = "Status = 'None'",
            SelectionDependencyType = MethodActionSelectionDependencyType.RequireSingleObject)]
        void Close(CloseInvoice convertParams);
        
        [System.ComponentModel.Browsable(false)]
        [VisibleInListView(false)]
        ICustomer BackRefCustomer { get; set; } //TODO: duplicated with IInvoce, IOpportunity, IOrder, IQuote
    }

    [DomainLogic(typeof(IInvoice))]
    public class InvoiceLogic {
        public static void AfterChange_BackRefCustomer(IInvoice saleBase) {
            saleBase.PotentialCustomer = saleBase.BackRefCustomer;
        }
        public static void AfterChange_PotentialCustomer(IInvoice saleBase) {
            saleBase.BackRefCustomer = saleBase.PotentialCustomer;
        }
    }
}
